﻿using System.Windows;
using System.Windows.Input;
using SABnzbdNET.Commands;
using SABnzbdNET.Events;

namespace SABnzbdNET.ViewModels
{
    public class SettingsViewModel : BaseViewModel
    {
        #region Fields and properties

        private IEventAggregator eventAggregator;
        private DelegateCommand closeWindowCommand;
        private DelegateCommand saveCommand;
        private string password;
        private bool autoStartUp;

        public ICommand CloseWindowCommand
        {
            get
            {
                if (closeWindowCommand == null)
                {
                    closeWindowCommand = new DelegateCommand(CloseWindow);
                }
                return closeWindowCommand;
            }
        }
        public ICommand SaveCommand
        {
            get
            {
                if (saveCommand == null)
                {
                    saveCommand = new DelegateCommand(SaveSettings);
                }
                return saveCommand;
            }
        }
        public string Password
        {
            get { return password; }
            set { password = value; OnPropertyChanged("Password"); }
        }
        public bool AutoStartUp
        {
            get { return autoStartUp; }
            set { autoStartUp = value; OnPropertyChanged("AutoStartUp"); }
        }

        #endregion

        public SettingsViewModel()
        {
            eventAggregator = EventAggregatorProvider.Instance;
            AutoStartUp = GetStartup("SABnzbdNET");
            Password = Properties.Settings.Default.ServerPassword;
        }

        private static void CloseWindow()
        {
            Application.Current.Windows[1].Close();
        }

        private void SaveSettings()
        {
            Properties.Settings.Default.ServerPassword = Password;
            Properties.Settings.Default.Save();
            SetStartup("SABnzbdNET", AutoStartUp);
            eventAggregator.Publish(new UpdateNotifyIconVisibilityMessage());
            eventAggregator.Publish(new UpdateServerInfoMessage());
            Application.Current.Windows[1].Close();
        }

        private static void SetStartup(string appName, bool enable)
        {
            const string runKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";

            Microsoft.Win32.RegistryKey startupKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(runKey);

            if (enable)
            {
                if (startupKey.GetValue(appName) == null)
                {
                    startupKey.Close();
                    startupKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(runKey, true);
                    startupKey.SetValue(appName, System.Reflection.Assembly.GetExecutingAssembly().Location);
                    startupKey.Close();
                }
            }
            else
            {
                startupKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(runKey, true);
                startupKey.DeleteValue(appName, false);
                startupKey.Close();
            }
        }

        private static bool GetStartup(string appName)
        {
            const string runKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
            Microsoft.Win32.RegistryKey startupKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(runKey);
            return startupKey.GetValue(appName) != null;
        }
    }
}
